//自动计算各种费用
function autoCalculate() {
	var rent = 0; //租金
	var margin = 0; //保证金
	var business_area = 0; //经营面积
	var other_charge = 0; //其他费用
	var dataGridRows = $("#tt").datagrid("getRows");
	for(var i = 0; i < dataGridRows.length; i++) {
		if(typeof(dataGridRows[i].RENT) != "undefined") {
			rent += parseFloat(dataGridRows[i].RENT);
			margin += parseFloat(dataGridRows[i].MARGIN);
			business_area += parseFloat(dataGridRows[i].BUSINESS_AREA);
			other_charge += parseFloat(dataGridRows[i].OTHERS_CHARGE);
		}
	}
	$("#input_RENT_SUM").numberbox('setValue', rent);
	$("#input_MARGIN_SUM").numberbox('setValue', margin);
	$("#input_BUSINESS_AREA").numberbox('setValue', business_area);
	$("#input_OTHERS_SUM").numberbox('setValue', other_charge);
}

//重构卡证与责任书的结构
function rebuildCardAndDutyData(dataGridRows, totalData, cardDetail) {
	for(var i = 0; i < dataGridRows.length; i++) {
		var _index = i;
		//责任书
		if(dataGridRows[i].NAME.indexOf(",") > 0) { //有多个责任书的情况
			var dutyInfo = dataGridRows[i].NAME.split(",");
			for(var j = 0; j < dutyInfo.length; j++) {
				totalData.rowsDuty[j] = new Object();
				totalData.rowsDuty[j].NAME = dutyInfo[j];
				totalData.rowsDuty[j].BOOST_ID = dataGridRows[i].BOOST_ID;
			}
		} else {
			totalData.rowsDuty[_index] = new Object();
			totalData.rowsDuty[_index].NAME = dataGridRows[i].NAME;
			totalData.rowsDuty[_index].BOOST_ID = dataGridRows[i].BOOST_ID;
		}
	}
	if(cardDetail.length == 0) {
		console.log("该摊位无卡证信息");
	} else {
		for(var i = 0; i < cardDetail.length; i++) {
			if(typeof(cardDetail[i]) != "undefined") {
				var _index = i;
				//卡证
				if(cardDetail[i].NAME.indexOf(",") > 0) { //有多个卡证的情况
					var cardsInfo = cardDetail[i].NAME.split(",");
					for(var j = 0; j < cardsInfo.length; j++) {
						totalData.rowsCard[j] = new Object();
						totalData.rowsCard[j].CARDNAME = cardsInfo[j];
						totalData.rowsCard[j].BOOST_ID = cardDetail[i].BOOST_ID;
						totalData.rowsCard[j].BOOSTCARD_ID = cardDetail[i].ID;
					}
				} else {
					totalData.rowsCard[_index] = new Object();
					totalData.rowsCard[_index].CARDNAME = cardDetail[i].NAME;
					totalData.rowsCard[_index].BOOST_ID = cardDetail[i].BOOST_ID;
					totalData.rowsCard[_index].BOOSTCARD_ID = cardDetail[i].ID;
				}
			}
		}
	}
	return totalData;
}

//确定后台接口以及数据
function confirmUrlAndData(opType, row, currid) {
	var _url;
	switch(opType) { //根据不同的状态来获取url
		case "startBoost": //启用摊位
			_url = RemoteLinkAddress + "/market/contract/boostexcute.do";
			_data = {
				"ID": row.ID,
				"BOOST_ID": row.BOOST_ID
			};
			break;
		case "stopBoost": //停用摊位
			_url = RemoteLinkAddress + "/market/contract/booststop.do";
			_data = {
				"ID": row.ID,
				"BOOST_ID": row.BOOST_ID
			};
			break;
		case "stopContract": //停止合同
			_url = RemoteLinkAddress + "/market/contract/stop.do";
			_data = {
				"ID": currid
			};
			break;
		case "stopShort"://停用短期合同项目
			_url = RemoteLinkAddress + "/market/contract/shortstop.do";
			_data = {
				"ID": row.ID
			};
			break;
		case "shortExcute"://启用短期合同项目
			_url = RemoteLinkAddress + "/market/contract/shortexcute.do";
			_data = {
				"ID": row.ID,
				"BOOST_ID": row.BOOST_ID
			};
			break;
	}
	launchRequest(_url, _data);
}

//执行Ajax与后台进行交互
function launchRequest(_url, _data) {
	$.ajax({
		dataType: 'JSON',
		url: _url,
		data: _data,
		type: "POST",
		success: function(result) {
			if(result.msg.search("success") > -1) {
				$.messager.alert("提示信息", "成功");
				getRefreshData();
			}
		},
		error: function(result) {
			$.messager.alert("错误信息", "失败");
		}
	});
}

//用于创建合同类型的选择
$("#input_CONTRACT_TYPE").combobox({
	url: RemoteLinkAddress + "/market/contracttype/listall",
	valueField: 'TYPE',
	textField: 'TYPE',
	panelHeight: 'auto'
});